﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIE.SCADA.ZemtDemo.Common
{
    public class Trackin
    {
        //条码解析
        //自制物料的二维码内容格式:N%物料编号%批次%序列号%内向交货单%供应商编码%DN号%销售订单%销售订单行项
        //外购物料的二维码内容格式：F%物料号%批次号或序列号+供应商代码+其他信息。
        public static CheckBarcode GetListBarcodeInfo(string Barcode)
        {
            CheckBarcode barcodeInfo;
            barcodeInfo = new CheckBarcode();
            try
            {
                if (Barcode.Contains("%"))
                {
                    string[] sArray = Barcode.Split('%');
                    if (sArray.Length == 0) return barcodeInfo;
                    if (sArray[0] == "N")
                    {
                        barcodeInfo.ModeNumber = sArray[1];
                        barcodeInfo.Lotnno = sArray[2];
                        barcodeInfo.SerialNumber = sArray[3];
                        barcodeInfo.WoNumber = sArray[4];
                        barcodeInfo.Supplier = sArray[5];
                        barcodeInfo.DNno = sArray[6];
                        barcodeInfo.SalesOrder = sArray[7];
                        barcodeInfo.SalesOrderNO = sArray[8];
                    }
                    else if (sArray[0] == "F")
                    {
                        barcodeInfo.ModeNumber = sArray[1];
                        barcodeInfo.SerialNumber = sArray[2];
                        //var temp = sArray[2];
                        //if (temp.Contains("+"))
                        //{
                        //        //string[] sArray1 = temp.Split('+');                          
                        //        //barcodeInfo.SerialNumber = sArray1[0];
                        //        //barcodeInfo.Supplier= sArray1[1];
                        //        var serialNo = temp.Split('+').FirstOrDefault();
                        //        barcodeInfo.SerialNumber = temp.Split('+').FirstOrDefault();
                        //        var sp = $"{serialNo}+";
                        //        var gy = temp.Replace(sp, "");
                        //        string reslut;
                        //        if (gy.Contains("+"))
                        //        {
                        //            reslut = gy.Split('+').FirstOrDefault();
                        //        }
                        //        else
                        //        {
                        //            reslut = gy;
                        //        }
                        //        barcodeInfo.Supplier = reslut;
                        // }
                        //else
                        //{
                        //    barcodeInfo.SerialNumber = temp;
                        //}
                    }
                    else if (sArray[0] == "M")
                    {
                        barcodeInfo.ModeNumber = sArray[1];
                    }
                }
                else if (Barcode.Contains("#"))
                {
                    string[] Array = Barcode.Split('#');
                    barcodeInfo.ModeNumber = Array.FirstOrDefault();
                    if (Array.Count() >= 2)
                    {
                        barcodeInfo.RepairNumber = Array[1];
                    }
                    barcodeInfo.SerialNumber = Array.LastOrDefault();
                }
                else
                {
                    barcodeInfo.SerialNumber = Barcode;
                }
                return barcodeInfo;
            }
            catch (Exception)
            {
                return barcodeInfo;

            }
        }
    }
}
